<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="id" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品标题" prop="productTitle">
        <el-input
          v-model="queryParams.productTitle"
          placeholder="请输入商品标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['spikes:spikes:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['spikes:spikes:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['spikes:spikes:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['spikes:spikes:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="spikesList" @selection-change="handleSelectionChange" >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="商品图片" align="center" prop="productImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.productImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="商品标题" align="center" prop="productTitle" />
      <el-table-column label="秒杀活动简介" align="center" prop="lightningDeals" />
      <el-table-column label="原价" align="center" prop="price" />
      <el-table-column label="秒杀价" align="center" prop="spikePrice" />
      <el-table-column label="限量" align="center" prop="limited" />
      <el-table-column label="限量剩余" align="center" prop="limitedRemaining" />
      <el-table-column label="秒杀状态" align="center" prop="lightningDealStatus" >
        <template slot-scope="scope">
          {{scope.row.lightningDealStatus==1?'进行中':'已结束'}}
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="eventTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.eventTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="活动状态" align="center" prop="activeStatus" >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.activeStatus"
            active-color="#13ce66"
            active-text="正常"
            inactive-text="未开启"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['spikes:spikes:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="cope(scope.row)"
            v-hasPermi="['spikes:spikes:edit']"
          >复制</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['spikes:spikes:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-loading"
            @click="handlecount(scope.row)"
          >统计</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品秒杀列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-steps :active="active" finish-status="success">
          <el-step title="步骤 1"></el-step>
          <el-step title="步骤 2"></el-step>
          <el-step title="步骤 3"></el-step>
        </el-steps>
        <div v-show="active==0||active==1" v-if="title=='添加商品秒杀列表'">
          <el-form-item label="选择商品">
            <div @click="handleSpxx()">
              <image-upload style="pointer-events: none" v-model="imageShow"></image-upload>
            </div>
          </el-form-item>
        </div>
        <el-dialog
          append-to-body
          title="提示"
          :visible.sync="dialogVisible"
          width="30%"
          :before-close="handleClose">
          <template>
            <el-table
              :data="tableData"
              stripe
              style="width: 100%"
              @selection-change="handleSelectionProduct">
              <el-table-column
                type="selection"
                width="55">
              </el-table-column>
              <el-table-column
                prop="id"
                label="商品id"
                width="180">
              </el-table-column>
              <el-table-column
                prop="image"
                label="商品图片"
                width="180">
                <template slot-scope="scope">
                  <image-preview :src="scope.row.image" :width="50" :height="50"/>
                </template>
              </el-table-column>
              <el-table-column
                prop="productName"
                label="商品信息">
              </el-table-column>
            </el-table>
          </template>
          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="setFrom">确 定</el-button>
        </span>
        </el-dialog>
        <div v-if="this.active==1">
        <el-form-item label="商品图片" prop="productImage">
          <image-upload v-model="form.productImage"/>
        </el-form-item>
        <el-form-item label="商品标题" prop="productTitle">
          <el-input v-model="form.productTitle" placeholder="请输入商品标题" />
        </el-form-item>
        <el-form-item label="秒杀活动简介" prop="lightningDeals">
          <el-input v-model="form.lightningDeals" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="活动时间" prop="eventTime">
          <el-date-picker clearable
            v-model="form.eventTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择活动时间">
          </el-date-picker>
        </el-form-item>
          <el-form-item label="物流方式" prop="logistics">
            <el-checkbox-group v-model="logistics">
              <el-checkbox label="快递" ></el-checkbox>
              <el-checkbox label="到店核销" ></el-checkbox>
            </el-checkbox-group>
          </el-form-item>
          <el-form-item label="运费设置" prop="shippingSettings">
            <el-radio-group v-model="form.shippingSettings">
              <el-radio  :label="1" >固定邮费</el-radio>
              <el-radio  :label="2">运费模板</el-radio>
            </el-radio-group>
          </el-form-item>
        <el-form-item label="固定邮费" prop="fixedPostage" v-if="form.shippingSettings==1">
          <el-input v-model="form.fixedPostage" placeholder="请输入固定邮费" />
        </el-form-item>
        <el-form-item label="运费模板" prop="shippingTemplates" v-if="form.shippingSettings==2">
          <el-select v-model="form.shippingTemplates" placeholder="请选择">
            <el-option
              v-for="item in productListSelectName"
              :key="item.id"
              :label="item.productName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-select v-model="form.startTime" placeholder="请选择活动区域">
            <el-option label="10点开始,持续5小时" value="10点开始,持续5小时"></el-option>
            <el-option label="15点开始,持续5小时" value="15点开始,持续5小时"></el-option>
            <el-option label="20点开始,持续4小时" value="20点开始,持续4小时"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="总购买数量限制" prop="totalPurchaseQuantityLimit">
          <el-input v-model="form.totalPurchaseQuantityLimit" placeholder="请输入总购买数量限制" />
        </el-form-item>
        <el-form-item label="单次购买数量限制" prop="onePurchaseQuantityLimit">
          <el-input v-model="form.onePurchaseQuantityLimit" placeholder="请输入单次购买数量限制" />
        </el-form-item>
          <el-form-item label="原价" prop="price">
            <el-input v-model="form.price" placeholder="请输入原价" />
          </el-form-item>
          <el-form-item label="限量剩余" prop="limitedRemaining">
            <el-input v-model="form.limitedRemaining" placeholder="请输入限量剩余" />
          </el-form-item>
          <el-form-item label="活动状态" prop="activeStatus">
            <el-switch
              style="display: block"
              v-model="form.activeStatus"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
              active-text="开启"
              inactive-text="关闭">
            </el-switch>
          </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序" />
        </el-form-item>
          <el-form-item label="秒杀是否参与分销" prop="spikesParticipateDistributionStatus">
            <el-switch
              style="display: block"
              v-model="form.spikesParticipateDistributionStatus"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
              active-text="开启"
              inactive-text="关闭">
            </el-switch>
          </el-form-item>
          <el-form-item label="秒杀价" prop="spikePrice">
            <el-input v-model="form.spikePrice" placeholder="请输入秒杀价" />
          </el-form-item>
          <el-form-item label="限量" prop="limited">
            <el-input v-model="form.limited" placeholder="请输入限量" />
          </el-form-item>
        <el-form-item label="规格id" prop="gid">
            <template v-if="this.active==1">
              <el-table :data="tableDataGg">
                <el-table-column
                  type="selection"
                  width="55">
                  <template slot-scope="scope">
                    <input type="radio"  v-model="form.gid" :value="scope.row.id"   @click="fgid(scope.row)"></input>
                  </template>
                </el-table-column>
                <el-table-column label="尺码" align="center" prop="cm" />
                <el-table-column label="秒杀价" align="center" prop="dhjf" >
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.dhjf" placeholder="请输入限量" />
                  </template>
                </el-table-column>
                <el-table-column label="颜色分类" align="center" prop="color" />
                <el-table-column label="图片" align="center" prop="tp" >
                  <template slot-scope="scope">
                    <image-preview :src="scope.row.tp" :width="50" :height="50"/>
                  </template>
                </el-table-column>
                <el-table-column label="库存" align="center" prop="kc" />
                <el-table-column label="限量" align="center" prop="xl" >
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.xl" placeholder="请输入限量" />
                  </template>
                </el-table-column>
                <el-table-column label="重量" align="center" prop="zl" />
                <el-table-column label="体积" align="center" prop="tj" />
              </el-table>
            </template>
          </el-form-item>
        </div>
        <div v-if="this.active==2">
          <el-form-item label="详情">
            <editor v-model="form.detail" :min-height="192"/>
          </el-form-item>
        </div>
      </el-form>
      <el-button style="margin-top: 12px;" @click="shang">上一步</el-button>
      <el-button style="margin-top: 12px;" @click="next">下一步</el-button>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSpikes, getSpikes, delSpikes, addSpikes, updateSpikes } from "@/api/spikes/spikes";
import {getPtsp} from "@/api/ptsp/ptsp";
import {ggList} from "@/api/integral/integral";
import {productList} from "@/api/comments/comments";

export default {
  name: "Spikes",
  data() {
    return {
      //自定义变量
      dialogVisible:false,
      imageShow:null,
      tableDataGg:[],
      tableData:[],
      productListSelectName:[],
      listGg:[],
      multipleSelection:[],
      //步骤条变量
      active:0,
      // 物流方式变量
      logistics:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品秒杀列表表格数据
      spikesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        productTitle: null,
        activeStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getSelectProduct();
    this.selectGg();
  },
  methods: {
    handlecount(row){
      this.$router.push({ path: "/ptgl/tj", query: { id: row.id} })
    },
    handleSelectionProduct(val){
      this.multipleSelection = val
      if (val.length > 1) {
        this.$refs.multipleTableFrag.clearSelection()
        this.$refs.multipleTableFrag.toggleRowSelection(val.pop())
      }
      console.log(val[0]['name'])
    },
    handleClose(){
      this.$confirm('确认关闭？')
        .then(_ => {
          this.dialogVisible=false
        })
        .catch(_ => {});
    },
    selectGg(){
      ggList(null).then(res=>{
        this.tableDataGg=res;
      })
    },
    getSelectProduct(){
      productList().then(res=>{
        this.tableData=res;
        this.productListSelectName=res
      })
    },
    fgid(row){
      this.form.gid=row.id;
      this.form.limited=row.xl;
      this.form.spikePrice=row.dhjf;
    },
    shang(){
      if (this.active-- > 0) this.active = this.active--;
    },
    next() {
      if (++this.active > 2) this.active = 0;
    },
    handleSpxx(){
      this.dialogVisible=true;
    },
    setFrom(){
      this.form.productImage=this.multipleSelection[0].image;
      this.form.productTitle=this.multipleSelection[0].productName;
      this.form.unit=this.multipleSelection[0].unit;
      this.form.sort=this.multipleSelection[0].sort;
      this.imageShow=this.multipleSelection[0].image;
      this.form.detail='<img src="'+this.multipleSelection[0].image+'">';
      this.dialogVisible=false;
    },
    cope(row){
      this.active=1;
      this.reset();
      const id = row.id || this.ids
      getSpikes(id).then(response => {
        this.form = response.data;
        this.form.logistics = this.form.logistics.split(",");
        this.form.id=null;
        this.open = true;
        this.title = "复制秒杀商品";
      });
    },
    /** 查询商品秒杀列表列表 */
    getList() {
      this.loading = true;
      listSpikes(this.queryParams).then(response => {
        this.spikesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        productImage: null,
        productTitle: null,
        lightningDeals: null,
        eventTime: null,
        logistics: [],
        shippingSettings: null,
        fixedPostage: null,
        shippingTemplates: null,
        startTime: null,
        totalPurchaseQuantityLimit: null,
        onePurchaseQuantityLimit: null,
        unit: null,
        sort: null,
        spikesParticipateDistributionStatus: null,
        activeStatus: null,
        gid: null,
        spikePrice: null,
        limited: null,
        detail: null,
        price: null,
        limitedRemaining: null,
        lightningDealStatus: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.active=0;
      this.reset();
      this.open = true;
      this.title = "添加商品秒杀列表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.active=1;
      this.reset();
      const id = row.id || this.ids
      getSpikes(id).then(response => {
        this.form = response.data;
        this.form.logistics = this.form.logistics.split(",");
        this.open = true;
        this.title = "修改商品秒杀列表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.logistics = this.form.logistics.join(",");
          this.form.detail=this.form.productImage;
          if (this.form.id != null) {
            updateSpikes(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSpikes(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商品秒杀列表编号为"' + ids + '"的数据项？').then(function() {
        return delSpikes(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('spikes/spikes/export', {
        ...this.queryParams
      }, `spikes_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
